package cn.smile.service.admin;

import java.util.List;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.stereotype.Service;

import cn.smile.bean.PageBean;
import cn.smile.dao.admin.OperaterLogDao;
import cn.smile.entity.admin.OperaterLog;
import cn.smile.entity.admin.User;
import cn.smile.util.SessionUtil;

import javax.annotation.Resource;

/**
 * 后台操作类 数据库操作service
 */
@SuppressWarnings("SpellCheckingInspection")
@Service
public class OperaterLogService {

    @Resource
    private OperaterLogDao operaterLogDao;

    /**
     * 添加/修改操作日志，当id不为空时，修改，id为空时自动新增一条记录
     */
    public OperaterLog save(OperaterLog operaterLog) {
        return operaterLogDao.save(operaterLog);
    }

    /**
     * 获取指定条数的操作日志列表
     */
    public List<OperaterLog> findLastestLog(int size) {
        return operaterLogDao.findLastestLog(size);
    }

    /**
     * 根据id查询单条数据
     */
    public OperaterLog findById(Long id) {
        return operaterLogDao.find(id);
    }

    /**
     * 返回所有的记录
     */
    public List<OperaterLog> findAll() {
        return operaterLogDao.findAll();
    }

    /**
     * 删除单条记录
     */
    public void delete(Long id) {
        operaterLogDao.deleteById(id);
    }

    /**
     * 清空整张表
     */
    public void deleteAll() {
        operaterLogDao.deleteAll();
    }

    /**
     * 操作日志添加
     */
    public void add(String operater, String content) {
        OperaterLog operaterLog = new OperaterLog();
        operaterLog.setOperator(operater);
        operaterLog.setContent(content);
        save(operaterLog);
    }

    /**
     * 操作日志添加
     */
    public void add(String content) {
        User loginedUser = SessionUtil.getLoginedUser();
        add(loginedUser == null ? "未知(获取登录用户失败)" : loginedUser.getUsername(), content);
    }

    /**
     * 分页查找日志
     */
    public PageBean<OperaterLog> findList(OperaterLog operaterLog, PageBean<OperaterLog> pageBean) {
        ExampleMatcher withMatcher = ExampleMatcher.matching().withMatcher("operator", GenericPropertyMatchers.contains());
        Example<OperaterLog> example = Example.of(operaterLog, withMatcher);
        Pageable pageable = PageRequest.of(pageBean.getCurrentPage() - 1, pageBean.getPageSize());
        Page<OperaterLog> findAll = operaterLogDao.findAll(example, pageable);
        pageBean.setContent(findAll.getContent());
        pageBean.setTotal(findAll.getTotalElements());
        pageBean.setTotalPage(findAll.getTotalPages());
        return pageBean;
    }

    /**
     * 操作日志总数
     */
    public long total() {
        return operaterLogDao.count();
    }
}
